# -*- coding: utf-8 -*-
import scrapy
from ipproxy.items import IpproxyItem


class ChongSpider(scrapy.Spider):
    name = 'chong'
    allowed_domains = ['www.bugng.com']

    def __init__(self, page_count=10, *args, **kwargs):
        super(ChongSpider, self).__init__(*args, **kwargs)
        self.start_urls = []
        for i in range(0, page_count+1):
            self.start_urls.append('http://www.bugng.com/gngn?page={0}'.format(i))

    def parse(self, response):
        all_trs = response.xpath('//*[@id="target"]/tr')
        for tr in all_trs[0:]:
            row = IpproxyItem()
            ipinfo = tr.xpath('td/text()').extract()
            row['ip'] = ipinfo[0]
            row['port'] = ipinfo[1]
            row['proxy_type'] = ipinfo[3]
            row['anonymous'] = ipinfo[2]
            row['country'] = ipinfo[5]
            row['checked_time'] = ipinfo[7]
            row['speed'] = ipinfo[6].split("秒")[0]
            row['proxy_name'] = self.name
            yield row
        pass